package net.cityside.jlearn.file;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

import org.apache.commons.lang3.StringUtils;

public class TraversalDirectory {

	public static void main(String[] args) throws FileNotFoundException {
		File root = new File("D:/Develop/study");
		if (!root.isDirectory()) {
			return;
		}
		PrintWriter pw = new PrintWriter(root.getPath() + "/out.txt");
		traversal(1, root, pw);
	}

	public static void traversal(int depth, File file, PrintWriter pw) {
		String prefix = StringUtils.repeat('-', depth) + " ";
		File[] children = file.listFiles();
		for (File child : children) {
			if (child.isDirectory()
					|| child.isFile()
					&& (child.getName()
							.matches(".*\\.(pdf|doc|docx|chm|zip|7z|rar)"))) {
				pw.println(prefix + child.getName());
			}
			if (child.isDirectory()) {
				traversal(depth + 1, child, pw);
			}
		}

	}
}
